package com.example.imooccommon.service.constsservice;

import com.example.imooccommon.entity.consts.ConstsCollege;
import com.example.imooccommon.repository.constsrepository.ConstsCollegeRepository;
import com.example.imooccommon.util.PageUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Anita
 * @Date 2018/1/16 18:14
 */
@Service
public class ConstsCollegeServiceImpl {

    @Autowired
    private ConstsCollegeRepository constsCollegeRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 根据id获取
     * @param id
     * @return
     */
    public ConstsCollege getById(Long id){
        return constsCollegeRepository.findByIdAndDel(id,0);
    }

    /**
     * 根据code获取
     * @param code
     * @return
     */
    public ConstsCollege getByCode(String code){
        return constsCollegeRepository.findByCode(code);
    }

    public List<ConstsCollege> queryAll(){
        return constsCollegeRepository.findAll();
    }

    /**
     * 名称分页获取
     * @param page
     * @param size
     * @return
     */
    public Page<ConstsCollege> queryPage(String name, int page,int size){

        String querySql ="select id, name,code,picture,create_time,create_user,update_time,update_user,del from consts_college where del=0 and name LIKE '%"+name+"%' order by id";
        RowMapper<ConstsCollege> rowMapper = new BeanPropertyRowMapper<>(ConstsCollege.class);
        List<ConstsCollege> constsColleges = jdbcTemplate.query(querySql,rowMapper);
        return new PageUtils<ConstsCollege>().getPage(constsColleges,page,size);
    }



    /**
     * 创建
     * @param constsCollege
     */
    public void create(ConstsCollege constsCollege){
        constsCollegeRepository.save(constsCollege);
    }

    /**
     *  删除
     *  @param constsCollege
     */
    public void delete(ConstsCollege constsCollege){
        constsCollegeRepository.delete(constsCollege.getId());
    }

    /**
     * 逻辑删除
     * @param constsCollege
     */
    public void deleteLogic(ConstsCollege constsCollege){
        constsCollegeRepository.updateDelByConstsCollegeId(constsCollege.getId());
    }






}
